
const fs = require('fs');

// 使用Babel解析器
function parseWithBabel(code) {
    const babel = require('@babel/parser');
    return babel.parse(code, {
        sourceType: 'unambiguous',
        allowImportExportEverywhere: true,
        allowReturnOutsideFunction: true,
        plugins: [
            'jsx',
            'typescript',
            'decorators-legacy',
            'classProperties',
            'objectRestSpread',
            'asyncGenerators',
            'dynamicImport',
            'exportDefaultFrom',
            'exportNamespaceFrom'
        ]
    });
}

// 主函数
function main() {
    const inputFile = process.argv[2];
    const outputFile = process.argv[3];
    
    if (!inputFile || !outputFile) {
        console.error('用法: node babel_parser_bridge.js <input.js> <output.json>');
        process.exit(1);
    }
    
    try {
        const code = fs.readFileSync(inputFile, 'utf8');
        const ast = parseWithBabel(code);
        
        const result = {
            success: true,
            ast: ast,
            parser: 'babel',
            error: null
        };
        
        fs.writeFileSync(outputFile, JSON.stringify(result, null, 2));
        
    } catch (error) {
        const result = {
            success: false,
            ast: null,
            parser: 'babel',
            error: error.message
        };
        fs.writeFileSync(outputFile, JSON.stringify(result, null, 2));
    }
}

main();
